package @{packagePath}.action;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jflyfox.dudu.component.base.BaseController;
import com.jflyfox.dudu.component.model.resubmit.SameUrlData;
import com.jflyfox.dudu.component.model.JqgridBean;
import @{packagePath}.model.@{crud.table.className};
import @{packagePath}.service.I@{strutils.toUpperCaseFirst(crud.urlKey)}Service;
import com.jflyfox.util.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @{crud.table.remarks} 控制层
 *
 * @author flyfox 369191470@qq.com on @{now}.
 */
@RestController
@RequestMapping(value = "/@{module}/@{crud.urlKey}")
public class @{strutils.toUpperCaseFirst(crud.urlKey)}Controller extends BaseController{

    @Autowired
    private I@{strutils.toUpperCaseFirst(crud.urlKey)}Service service;

    private static final String path = "/pages/@{module}/@{crud.urlKey}/@{crud.urlKey}_";

    @RequestMapping(value = "/list")
    public ModelAndView list() {
        ModelAndView view = new ModelAndView(path + "list.html");
        return view;
    }

    @RequestMapping(value = "/add")
    public ModelAndView add() {
        ModelAndView view = new ModelAndView(path + "add.html");
        return view;
    }

    @RequestMapping(value = "/edit/{id}")
    public ModelAndView edit(@PathVariable Long id) {
        @{crud.table.className} model = service.selectById(id);
        ModelAndView view = new ModelAndView(path + "edit.html");
        view.addObject("model",model);
        return view;
    }

    @RequestMapping(value = "/view/{id}")
    public ModelAndView view(@PathVariable Long id) {
        @{crud.table.className} model = service.selectById(id);
        ModelAndView view=new ModelAndView(path + "view.html");
        view.addObject("model",model);
        return view;
    }

    @SameUrlData
    @RequestMapping(value = "/delete/{id}")
    public Object delete(@PathVariable Long id) {
        if (!service.deleteById(id)) {
            return fail("删除失败");
        }

        return success();
    }

    @SameUrlData
    @RequestMapping(value = "/save/{id}")
    public Object save(@PathVariable Long id,@ModelAttribute @{crud.table.className} model) {
        Long userid = getSessionUser().getUserid();
        String now = getNow();
        model.setUpdateId(userid);
        model.setUpdateTime(now);
        if (id!=null&&id>0) { // 更新
            service.updateById(model);
        } else { // 新增
            model.setCreateId(userid);
            model.setCreateTime(now);
            service.insert(model);
        }

        return success();
    }

    @RequestMapping(value = "/jqgrid")
    public Object jqgrid(JqgridBean bean, @ModelAttribute @{crud.table.className} attr) {
        Wrapper<@{crud.table.className}> wrapper = new EntityWrapper<>();
//        if(StrUtils.isNotEmpty(attr.getName())) {
//            wrapper.like("t.name",attr.getName());
//        }
        if (StrUtils.isNotEmpty(bean.getOrderBy())) {
            wrapper.orderBy("t." + bean.getOrderBy());
        }

        Page<@{crud.table.className}> pageData = service.select@{strutils.toUpperCaseFirst(crud.urlKey)}Page(bean.getPagination(), wrapper);

        return getJqgridList(pageData);
    }

}